//
//  GameDetailController.swift
//  appstore
//
//  Created by llbt-sk on 2019/11/7.
//  Copyright © 2019 Roy. All rights reserved.
//

import UIKit

class GameDetailController: BaseViewController,UITableViewDataSource,UITableViewDelegate {

    let GameInfoCellIdentifier = "GameInfoCellIdentifier"
    let GameNewCellIdentifier = "GameNewCellIdentifier"
    let GameReviewCellIdentifier = "GameReviewCellIdentifier"
    let GameDetailInfoCellIdentifier = "GameDetailInfoCellIdentifier"
    
    var tableView:UITableView!
    var headerImageView:UIImageView!
    var headerView:UIView!
    
    let data: [(String,String)] = [
        ("Seller", "Hangzhou NetEase Leihuo Technology Co., Ltd."),
        ("Size", "2.5GB"),
        ("Category", "Games: Strategy"),
        ("Compatibility", "Works on this iphone"),
        ("Languages", "Simplified Chinese"),
        ("Age Rating", "9+"),
        ("In-App Purchases", "Yes"),
        ("Copyright", "©1997-2019 网易公司版权所有")
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.initView()
    }
    
    func initView() -> Void {

        tableView = UITableView(frame: CGRect.zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorInset = UIEdgeInsets(top: 0.0, left: 20.0, bottom: 0.0, right: 20.0)
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.top.bottom.left.right.equalTo(0.0)
        }
        tableView.contentInsetAdjustmentBehavior = .never
        
        tableView.register(GameInfoCell.self, forCellReuseIdentifier: GameInfoCellIdentifier)
        tableView.register(NewCell.self, forCellReuseIdentifier: GameNewCellIdentifier)
        tableView.register(ReviewCell.self, forCellReuseIdentifier: GameReviewCellIdentifier)
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DefeaultTableViewCellIdentifier")
        tableView.register(GameDetailInfoCell.self, forCellReuseIdentifier: GameDetailInfoCellIdentifier)
        
        //
        headerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: ScreenWidth, height: 200.0))
        tableView.tableHeaderView = headerView
        
        headerImageView = UIImageView(frame: CGRect(x: 0.0, y: -100.0, width: ScreenWidth, height: 300.0))
        headerImageView.image = UIImage(named: "cover_detail")
        headerImageView.contentMode = .scaleAspectFill
        headerImageView.layer.masksToBounds = true
        headerView.addSubview(headerImageView)
    }
    
    // UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4 + data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: GameInfoCellIdentifier)
            return cell!
        } else if indexPath.row == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: GameNewCellIdentifier)
            return cell!
        } else if indexPath.row == 2 {
            let cell = tableView.dequeueReusableCell(withIdentifier: GameReviewCellIdentifier)
            return cell!
        } else if indexPath.row == 3 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "DefeaultTableViewCellIdentifier")
            cell?.textLabel?.text = "Information"
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 20.0)
            cell!.separatorInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: ScreenWidth)
            return cell!
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: GameDetailInfoCellIdentifier) as! GameDetailInfoCell
            cell.nameLabel.text = data[(indexPath.row - 4)].0
            cell.contentLabel.text = data[(indexPath.row - 4)].1
            return cell
        }
    }
    
    // UITableViewDelegate
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 230.0
        } else if indexPath.row == 1 {
            return 168.0
        } else if indexPath.row == 2 {
            return 227.0
        } else {
            return 50.0
        }
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let offsetY = scrollView.contentOffset.y
        if offsetY < 0 {
            let originalHeight = headerImageView.frame.height

            let scale:CGFloat = (originalHeight - offsetY) / originalHeight
            let transformScale3D:CATransform3D = CATransform3DMakeScale(scale, scale, 1.0)
            let translation3D:CATransform3D = CATransform3DMakeTranslation(0, offsetY/2, 0)
            headerImageView.layer.transform = CATransform3DConcat(transformScale3D, translation3D)

        } else {
            headerImageView.layer.transform = CATransform3DIdentity
        }
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
